package com.example.drive.audit.controller;


import com.example.drive.admin.pojo.dto.UpdatePwdDTO;
import com.example.drive.admin.pojo.vo.UserLoginVO;
import com.example.drive.audit.pojo.dto.AuditDTO;
import com.example.drive.audit.pojo.entity.Audit;
import com.example.drive.audit.service.AuditService;
import com.example.drive.common.JWTUtils;
import com.example.drive.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 审核员控制器
 */
@RestController
@RequestMapping("audit")
public class AuditController {

     @Autowired
    private AuditService auditService;

    /**
     * 修改密码
     * @param updatePwdDTO
     * @return
     */
    @PutMapping("change")
    public R<Void> ChangePassword(@RequestBody @Validated UpdatePwdDTO updatePwdDTO, HttpServletRequest request){
        // 获取用户的身份：请求头中是含有token的
        String token = request.getHeader("Authorization");
        // 解析token：知道用户的身份
        UserLoginVO loinUser = JWTUtils.parseToken(token);
        auditService.change(updatePwdDTO,loinUser);
        return R.ok();
    }

    /**
     * 更新个人信息
     * @param auditDTO
     * @param request
     * @return
     */
    @PutMapping("update")
    public R<Void> updateInfo(@RequestBody @Validated AuditDTO auditDTO, HttpServletRequest request){
        // 获取用户的身份：请求头中是含有token的
        String token = request.getHeader("Authorization");
        // 解析token：知道用户的身份
        UserLoginVO loinUser = JWTUtils.parseToken(token);
        auditService.updateInfo(auditDTO,loinUser);
        return R.ok();
    }

    /**
     * 显示个人信息
     * @param id
     * @return
     */
    @GetMapping("search/{id}")
    public R<Audit> search(@PathVariable Integer id){
        Audit audit = auditService.search(id);
        return R.ok(audit);
    }



}
